package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.system.domain.Lesson;
import com.ruoyi.system.domain.Sign;
import com.ruoyi.system.mapper.LessonMapper;
import com.ruoyi.system.mapper.SignMapper;
import com.ruoyi.system.service.LessonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LessonServiceImpl implements LessonService {
    @Autowired private LessonMapper lessonMapper;
    @Autowired private SignMapper signMapper;

    @Override
    public List<Lesson> selectAll() {
        LambdaQueryWrapper<Lesson> wrapper = new LambdaQueryWrapper<>();
        return this.lessonMapper.selectList(wrapper);
    }

    @Override
    public Lesson selectById(Integer lessonId) {
        return this.lessonMapper.selectById(lessonId);
    }

    @Override
    public Lesson selectLesson(Integer lessonId, Integer studentId) {
        Lesson lesson = this.lessonMapper.selectById(lessonId);
        if (lesson == null) {
            return null;
        }
        LambdaQueryWrapper<Sign> wrapper = new LambdaQueryWrapper<Sign>();
        wrapper.eq(Sign::getLessonId,lessonId)
                .eq(Sign::getStudentId,studentId);
        boolean hasSigned = this.signMapper.selectCount(wrapper) > 0;
        lesson.setHasSigned(hasSigned);
        return lesson;
    }
}
